#include "stdafx.h"
#include "mystring.h"
#include <cstdlib>
#include <cstring>
mystring::mystring(void)
{
	this->clear();
}
mystring::mystring(const char *instr)
{
	strcpy(this->str,instr);
}
void mystring::fromDouble(double doubleVal){
	sprintf(this->str,"%.16g",doubleVal);
}
void mystring::append(const char ch){
	int length=this->size();
	this->str[length++]=ch;
	this->str[length]='\0';
	return ;
}
void mystring::append(const char *s){
	strcat(this->str,s);
	printf(s);
}
void mystring::deletelast(){
	int length=this->size();
	if(length){//>0
	this->str[--length]='\0';
	}
}
void mystring::deletefirst(){
	char tmp[128];
	strcpy(tmp,this->str+1);
	strcpy(this->str,tmp);
}
void mystring::insertfirst(const char ch){
	char tmp[128];
	strcpy(tmp,this->str);
	strcpy(&this->str[1],tmp);
	this->str[0]=ch;
}
char *mystring::c_str(){
	return this->str;
}

mystring::~mystring(void)
{
	//Do nothing...
}
double mystring::toDouble(){
	return atof(this->str);
}
int mystring::compare(mystring another){
	return compare(another.c_str());
}
int mystring::compare(const char *a){
	int i=0;
	while(true){
		char c1=str[i];
		char c2=a[i];
		if (c1!=c2)
		{
			return c1-c2;
		}
		if (c1==c2 && c1==0)
		{
			return 0;
		}
		i++;

	}
}
/************************************************************************/
/* bad coding....                                                       */
/************************************************************************/
void mystring::substr(int pos,int len){
	char tmp[128];
	strncpy(tmp,this->str+pos,len);
	strcpy(this->str,tmp);
}
void mystring::clear(){
	this->str[0]='\0';
}
int mystring::size(){
	return strlen(this->str);
}
int mystring::at(int pos){
	return this->str[pos];
}
bool mystring::constain(const char ch){
	int c;
	int i;
	while(c=this->str[i++]){
		if (c==ch)
		{
			return true;
		}
	}
	return false;
}